﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using KioskosJugandoLimpio.Classes;

namespace KioskosJugandoLimpio.Admin
{
    public partial class Graficas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
              String vl = "";
              
              Control prueba = new Control();
            if (null != Request.QueryString["idEncuestas"])
            {
                SqlConnection cn, cn2,cn3,cn4;
                SqlCommand cmd, cmd2, cmd3,cmd4;
                SqlDataReader dr, dr2, dr3,dr4;

                String ConFigString = "Data Source=200.78.237.212;Initial Catalog=kioskos;Persist Security Info=True;User ID=sa;Password=kjl";
                cn = new SqlConnection(ConFigString);
                cn2 = new SqlConnection(ConFigString);
                cn3 = new SqlConnection(ConFigString);
                cn4 = new SqlConnection(ConFigString);

                select.Text = "";
                String sel = "<inpur type=\"hidden\" name=\"idEncuestas\" value=\"" + Request.QueryString["idEncuestas"].ToString() + "\"> <select name=\"id_pregunta\" style=\" width:200px;\" >";

                String Sql = "select * from Preguntas where Encuestas_idEncuestas='" + Request.QueryString["idEncuestas"].ToString() + "' and TiposPreguntas_idTiposPreguntas !='3'";

                try
                {
                    cn.Open();
                    cn2.Open();
                    cn3.Open();
                    cn4.Open();
                    cmd = new SqlCommand(Sql, cn);
                    dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        sel += "<option value='" + dr["idPreguntas"] + "'>" + dr["Pregunta"] + "</option>";

                    }


                    sel += "</select>";
                    
                    select.Text = sel ;
                    
                    if (null != this.Request.Form["id_pregunta"])
                    {
                       


                        String Sql2 = "select Preguntas.idPreguntas, Opciones.idOpciones, Opciones.Opcion  from Preguntas left join OpcionesPreguntas on Preguntas.idPreguntas=OpcionesPreguntas.Preguntas_idPreguntas left join Opciones on OpcionesPreguntas.Opciones_idOpciones=Opciones.idOpciones where Preguntas.idPreguntas=" + this.Request.Form["id_pregunta"].ToString();
                       
                        cmd3 = new SqlCommand(Sql2, cn3);
                        dr3 = cmd3.ExecuteReader();

                        String Sql5 = "select * from Preguntas where idPreguntas='" + this.Request.Form["id_pregunta"].ToString() + "' ";
                        cmd4= new SqlCommand(Sql5, cn4);
                        dr4 = cmd4.ExecuteReader();
                        dr4.Read();
                        
                        vl += "var chart; ";
			            vl += "$(document).ready(function() { \n";
				        vl += " chart = new Highcharts.Chart({ \n";
					    vl += " chart: { ";
                        vl += " renderTo: 'grafica', ";
						vl += " defaultSeriesType: 'bar' ";
				    	vl += " }, ";
					    vl += " title: { ";
                        vl += " text: '" + dr4["Pregunta"] + " ' ";
					    vl += " }, ";
					    vl += " subtitle: { ";
						vl += " text: 'Kiosko' ";
					    vl += " }, ";
					    vl += " xAxis: { ";                        
						vl += " categories: [''], ";

						vl += " title: { ";
						vl += " text: null ";
						vl += " } ";
					    vl += " }, ";
					    vl += " yAxis: { ";
						vl += " min: 0, ";
						vl += " title: { ";
						vl += "	text: 'Conteo', ";
						vl += "	align: 'high' ";
						vl += " } ";
					    vl += " }, ";
					    vl += " tooltip: { ";
						vl += " formatter: function() { ";
						vl += "	return ''+ ";
                        vl += " this.series.name +': '+ this.y +''; ";
						vl += " } ";
					    vl += " }, ";
					    vl += " plotOptions: { ";
						vl += " bar: { ";
						vl += "	dataLabels: { ";
						vl += " 		enabled: true ";
						vl += "	} ";
						vl += " } ";
					    vl += " }, ";
					    vl += " legend: { ";
						vl += " layout: 'vertical', ";
						vl += " align: 'right', ";
						vl += " verticalAlign: 'top', ";
						vl += " x: -100, ";
						vl += " y: 100, ";
						vl += " floating: true, ";
						vl += " borderWidth: 1, ";
						vl += " backgroundColor: '#FFFFFF', ";
						vl += " shadow: true ";
					    vl += " }, ";
					    vl += " credits: { ";
						vl += " enabled: false ";
					    vl += " }, ";
                        vl += " series: [ ";


                       // script.Text += vl + "hola";
                        while (dr3.Read())
                        {
                            String Sql3 = "select COUNT(idRespuestas) as red from Respuestas where Respuestas.Opciones_idOpciones=" + dr3["idOpciones"] + " and Respuestas.Preguntas_idPreguntas=" + this.Request.Form["id_pregunta"].ToString();
                           
                            cmd2 = new SqlCommand(Sql3, cn2);
                            dr2 = cmd2.ExecuteReader();

                            if(dr2.Read())
                            {
                                vl += "{name: '" + dr3["Opcion"] + "', data: [" + dr2["red"] + "]}, ";
                            }

                            dr2.Close();
                        }
                       


					    vl += " ] ";
				        vl += " }); ";
				
				
			vl += "}); ";

                  
                     
                      
                    }

                    ClientScript.RegisterStartupScript(this.GetType(), "CodJavaScript2", vl,true);
                    //ScriptManager.RegisterClientScriptBlock(this, typeof(string), "ScriptManager1", "", true);
                   
                }
                catch (Exception ex)
                {
                    select.Text += "" + ex.Message;
                }
                finally
                {
                    cn.Close();
                    
                }
                



               
            
                

            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}